#####
## September 2019
## Run this code file fourth in R
## This generates analyses for appendix
#####

set.seed(43)

library(foreign)
library(dplyr)
library(stargazer)
library(pscl)
library(haven)

data<-read_dta(file="Survey_Data_Cleaned.dta")
scaled_estimates_screeners<-read.dta(file="scaled_estimates_screeners.dta")

data2<-dplyr::select(data, pk1, pk2, pk3, pk4, pk5)
data2<-rollcall(data2,
yea=1, nay=0, missing=c(NA), notInLegis=9,
legis.names=NULL, vote.names=NULL,
legis.data=NULL, vote.data=NULL,
desc=NULL, source=NULL)
knowledge<-ideal(data2, store.item=T,normalize=T,maxiter = 20000, thin = 10)
estimates_knowledge<-cbind(data$respondent,as.data.frame(summary(knowledge)[2]))
colnames(estimates_knowledge)<-c("respondent", "political_knowledge")
estimates_knowledge<-as.data.frame(estimates_knowledge)

data<-merge(data, scaled_estimates_screeners, by="respondent")
data<-merge(data, estimates_knowledge, by="respondent")
## polarity of knowledge scale is abritrary. Might need to be flipped for positive to mean greater knowledge.
#data$political_knowledge<-data$political_knowledge*-1
cor(data$political_knowledge, data$pkav, use="complete.obs")

reg1<-lm(attention_fullscale~political_knowledge+somecollege+ college+age+female+white+black+hispanic , data=data)
reg2<-lm(attention_nongrid~political_knowledge+somecollege+ college+age+female+white+black+hispanic , data=data)
reg3<-lm(attention_grid~political_knowledge+somecollege+ college+age+female+white+black+hispanic , data=data)
reg4<-lm(attention_mixed~political_knowledge+somecollege+ college+age+female+white+black+hispanic , data=data)

reg5<-lm(attention_fullscale~somecollege+ college+age+female+white+black+hispanic , data=data)
reg6<-lm(attention_nongrid~somecollege+ college+age+female+white+black+hispanic , data=data)
reg7<-lm(attention_grid~somecollege+ college+age+female+white+black+hispanic , data=data)
reg8<-lm(attention_mixed~somecollege+ college+age+female+white+black+hispanic , data=data)

## Table A1
stargazer(reg5, reg6, reg7, reg8)

## Table A2
stargazer(reg1, reg2, reg3, reg4)
